<template>
	<view>
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" :options="formData" :collection="collection"
		 :field="field" @load="load" orderby="_id desc" where="isApproval==true&&uid==$env.uid">
			<view class="cu-list menu-avatar">
				<view class="cu-item" v-for="item in data" :key="item._id">
					<view class="cu-avatar round lg bg-brown" :style="'background-image:url('+item.imgPath[0]+');'" @click.stop="previewPic(item.imgPath)"/>
					<view class="content">
						<view class="text-grey">{{item.picActivityTitle}}</view>
						<view class="text-gray text-sm flex">
							<uni-dateformat :date="item.timestamp" ></uni-dateformat>
						</view>
					</view>
					<view>
						第{{item.times}}次打卡
					</view>
					<view v-if="!item.send">
						<button size="mini" @click="toApproval(item._id)" v-if="!item.isApproval">审核</button>
					</view>
				</view>
			</view>
			<uni-load-more v-if="loading || options.status === 'noMore' " :status="options.status" />
		</unicloud-db>
	</view>
</template>

<script>
	const app = getApp();
	const db = uniCloud.database();
	export default {
		data() {
			return {
				collection: 'picToScores',
				field: '_id, isApproval, wx_openid, timestamp, picActivityTitle, imgPath, times',
				formData: {
					status: 'loading', // 加载状态
				},
				dbWhere: {
						isApproval: true
					},
				picActList: []
			}
		},
		
		methods: {
			/**
			 * 下拉刷新回调函数
			 */
			onPullDownRefresh() {
				this.tipShow = true
				this.formData.status = 'more'
				this.$refs.udb.loadData({
					clear: true
				}, () => {
					this.tipShow = false
					uni.stopPullDownRefresh()
				})
			},
			/**
			 * 上拉加载回调函数
			 */
			onReachBottom() {
				this.$refs.udb.loadMore()
			},
			
			load(data,ended){
				if(ended){
					this.formData.status = 'noMore'
				}
			},
			
			toApproval : function(id) {
				uniCloud.database().collection('picToScores')
				.doc(id)
				.update({
					isApproval: true
				})
			},
			
			previewPic: function(imgPath) {
				uni.previewImage({
					urls: imgPath
				})
			},
			
			onLoad: function() {
				if (!app.globalData.hasLogin) {
					uni.showModal({
						title: "请先登录",
						mask: true,
						success: (res) => {
							if(res.confirm){
								uni.switchTab({
									url:"/pages/profile/profile"
								})
							} else {
								uni.navigateBack({
									delta: 1
								})
							}
						}
					});
				}
				db.collection('picActivities').orderBy('starttime asc, endtime asc').get().then(res => {
					this.picActList = res.result.data
					res.result.data.forEach(d => {
						
					})
				})
			}
		}
	}
</script>

<style>

</style>
